﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Ataric.SuppressWP.Model;
using Microsoft.SharePoint.WebPartPages;
using Ataric.SuppressWP.Utilities;

namespace Ataric.SuppressWP
{
    [ToolboxItemAttribute(false)]
    public class SuppressWP : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable
    {
        #region  Properties
        [Category("Suppress Settings")]
        [FriendlyNameAttribute("Suppress Items")]
        [Description("")]
        [Browsable(true)]
        [XmlElement(ElementName = "Suppressitems")]
        [Personalizable(PersonalizationScope.Shared)]
        // The accessor for this property.
        public SuppressItemCollection SuppressItems
        {
            get;
            set;
        }
        #endregion
        protected override void CreateChildControls()
        {
            WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(this.Page);
            if (mgr.DisplayMode.AllowPageDesign || mgr.DisplayMode == WebPartManager.EditDisplayMode || mgr.DisplayMode == WebPartManager.DesignDisplayMode)
            {
                this.Controls.Add(new Literal() { Text = "This webpart doesn't render in Edit Mode & Design Mode" });
                return;
            }
            this.ChromeType = PartChromeType.None;
            StringBuilder sb = new StringBuilder();
            string source = CommonHelper.LoadResource("Ataric.SuppressWP.SuppressWP", "SuppressItems.xml");
            SuppressSettings setting = SerializationHelper.XmlDeserialize<SuppressSettings>(source);

            
            if (this.SuppressItems != null)
            {
                sb.AppendLine("<style type=\"text/css\">");
                this.SuppressItems.ForEach(delegate(SuppressItem item)
                {
                    sb.AppendLine(string.Format("{0}{1}display:none !important;{2}",item.CSSClass ,'{','}'));
                    if (!string.IsNullOrEmpty(item.CSSExtra)) sb.AppendLine(item.CSSExtra);
                });

                foreach (var group in setting.Where(p=>!string.IsNullOrEmpty(p.CSSExtra)))
                {
                    if (group.SuppressItems.All(p => this.SuppressItems.Exists(c => c.Name == p.Name)))
                    {
                        sb.AppendLine(group.CSSExtra);
                    }
                }

                sb.AppendLine("</style>");
                ContentPlaceHolder holder = Page.Master.FindControl("PlaceHolderAdditionalPageHead") as ContentPlaceHolder;
                if (holder != null)
                {
                    holder.Controls.Add(new Literal() { Text = sb.ToString() });
                }
            }

            
        }
        #region Editor Part
        
        EditorPartCollection IWebEditable.CreateEditorParts()
        {
            List<EditorPart> editors = new List<EditorPart>();
            //editors.AddRange();
            SuppressItemsEditorPart item = new SuppressItemsEditorPart();
            item.ID += "_" + this.ClientID;
            editors.Add(item);
            EditorPartCollection editorParts =
              new EditorPartCollection(editors.ToArray());
            return editorParts;
        }

        object IWebEditable.WebBrowsableObject
        {
            get { return this; }
        }
        #endregion
    }
}
